package ua.com.minus1.lookbattle;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ViewFlipper;

import ua.com.minus1.lookbattle.models.CommonResult;
import ua.com.minus1.lookbattle.net.HttpManager;
import ua.com.minus1.lookbattle.net.URLManager;
import ua.com.minus1.lookbattle.utils.AppPreferences;

/**
 * Created with IntelliJ IDEA.
 * User: Flash
 * Date: 05.09.12
 * Time: 12:42
 * To change this template use File | Settings | File Templates.
 */
public class LoginView  extends ViewFlipper implements View.OnClickListener {

    private final int LOGIN_OK = 0;
    private final int LOGIN_EMAIL_EXISTS = 26;

    private EditText emailEdit;
    private EditText usernameEdit;
    private TextView sendCodeEmailText;
    private EditText codeEdit;
    private OnRegisterListener onRegisterListener;
    private HttpManager httpManager;
    private AppPreferences preferences;
    private String uuid;

    //TODO: hide keyboard after registration

    public LoginView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void inflate() {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View content = inflater.inflate(R.layout.fragment_login, this);

        preferences = new AppPreferences(getContext());
        uuid = preferences.getUUID();
        httpManager = new HttpManager(getContext());

        setupViews();
        setDisplayedChild(0);
    }

    private void setupViews() {
        emailEdit = (EditText) findViewById(R.id.loginEmail);
        usernameEdit = (EditText) findViewById(R.id.loginUsername);
        sendCodeEmailText = (TextView) findViewById(R.id.sendCodeEmailText);
        codeEdit = (EditText) findViewById(R.id.loginCode);

        Button registerBtn = (Button) findViewById(R.id.loginRegisterBtn);
        Button cancelRegisterBtn = (Button) findViewById(R.id.loginCancelRegisterBtn);
        Button codeSendBtn = (Button) findViewById(R.id.loginCodeSendBtn);
        Button cancelCodeSendBtn = (Button) findViewById(R.id.loginCancelCodeSendBtn);
        Button codeRegisterBtn = (Button) findViewById(R.id.loginCodeRegisterBtn);
        Button cancelCodeRegisterBtn = (Button) findViewById(R.id.loginCancelCodeRegisterBtn);

        Button[] btns = {registerBtn, cancelRegisterBtn, codeSendBtn, cancelCodeSendBtn, codeRegisterBtn, cancelCodeRegisterBtn};
        for (Button btn : btns) {
            btn.setOnClickListener(this);
        }
    }


    public void setOnRegisterListener(OnRegisterListener listener) {
        onRegisterListener = listener;
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.loginRegisterBtn:
                register();
                break;
            case R.id.loginCancelRegisterBtn:
                onRegisterListener.onRegister(false);
                break;
            case R.id.loginCodeSendBtn:
                sendCode();
                break;
            case R.id.loginCancelCodeSendBtn:
                reset();
                break;
            case R.id.loginCodeRegisterBtn:
                confirmCode();
                break;
            case R.id.loginCancelCodeRegisterBtn:
                reset();
                break;
        }
    }

    public void register() {
        httpManager.loadClass(URLManager.getRegisterURL(uuid, emailEdit.getText().toString(), usernameEdit.getText().toString()), CommonResult.class, new HttpManager.HttpResultHandler<CommonResult>() {
            public void onResultLoadComplete(CommonResult result) {
                Log.i(LoginView.class.getName(), "Registration result: " + result.toString());

                switch (result.status) {
                    case LOGIN_OK:
                        onRegisterListener.onRegister(true);
                    break;
                    case LOGIN_EMAIL_EXISTS:
                        sendCodeEmailText.setText(emailEdit.getText());
                        setDisplayedChild(1);
                    break;
                }
            }
        });
    }

    public void sendCode() {
        httpManager.loadClass(URLManager.getSendCodeURL(uuid, emailEdit.getText().toString()), CommonResult.class, new HttpManager.HttpResultHandler<CommonResult>() {
            public void onResultLoadComplete(CommonResult result) {
                Log.i(LoginView.class.getName(), "Send code result: " + result.toString());
                setDisplayedChild(2);
            }
        });
    }

    private void confirmCode() {
        Log.i(LoginView.class.getName(), "Confirm code url " + URLManager.getConfirmCode(uuid, codeEdit.getText().toString()));
        httpManager.loadClass(URLManager.getConfirmCode(uuid, codeEdit.getText().toString()), CommonResult.class, new HttpManager.HttpResultHandler<CommonResult>() {
            public void onResultLoadComplete(CommonResult result) {
                Log.i(LoginView.class.getName(), "Confirm code result: " + result.toString());
                if (result.status == 0) {
                    preferences.setClicks(result.clicks);
                    onRegisterListener.onRegister(true);
                } else
                    onRegisterListener.onIncorrectCode();

            }
        });
    }

    public void reenterCode() {
        codeEdit.setText("");
    }

    public void reset() {
        emailEdit.setText("");
        usernameEdit.setText("");
        codeEdit.setText("");
        setDisplayedChild(0);
    }

    public interface OnRegisterListener {
        void onRegister(boolean success);
        void onIncorrectCode();
    }
}
